#include "stm32f10x.h"
#include "nvic.h"
#include "can.h"
#include "rs485.h"
#include "tim.h"
#include "ioport.h"
#include "led.h"
#include "globalvariable.h"
extern uint8_t  RS485_Message[200];
uint16_t ac_Torque;
uint32_t position0 = 0;
int32_t position1 = 0;
int32_t position2 = 0;
int32_t position3 = 0;
int32_t position4 = 0;
int32_t position5 = 0; 
int32_t acposition = 0;
uint16_t NowGroup = 0;
uint8_t up = 0;
uint8_t down = 0;
volatile u32 time; // ms ��ʱ����
extern int32_t plus;		//������
extern uint8_t kill;
uint8_t flash_number = 0;

uint8_t hold_x1 = 0;
uint8_t hold_x2 = 0;
uint8_t hold_x3 = 0;
uint8_t hold_x4 = 0;

/* Hold signal; hold input signal unless modbus master geted  input singal*/
void check_X(void)
{
	if(Input_X1() == 1){hold_x1 = 1;}
	if(Input_X2() == 1){hold_x2 = 1;}
	if(Input_X3() == 1){hold_x3 = 1;}
	if(Input_X4() == 1){hold_x4 = 1;}
	
	if(hold_x1 == 0 && hold_x2 == 0&& hold_x3 == 0&& hold_x4 == 0)    
	{ModBus_RegisterData[0x0001]=0;}
	else if(hold_x1 == 1 && hold_x2 == 0&& hold_x3 == 0&& hold_x4 == 0)    
	{ModBus_RegisterData[0x0001]=1;}
	else if(hold_x1 == 0 && hold_x2 == 1&& hold_x3 == 0&& hold_x4 == 0)   
	{ModBus_RegisterData[0x0001]=2;}						
	else if(hold_x1 == 1 && hold_x2 == 1&& hold_x3 == 0&& hold_x4 == 0)    
	{ModBus_RegisterData[0x0001]=3;}   
	else if(hold_x1 == 0 && hold_x2 == 0&& hold_x3 == 1&& hold_x4 == 0)    
	{ModBus_RegisterData[0x0001]=4;}
	else if(hold_x1 == 1 && hold_x2 == 0&& hold_x3 == 1&& hold_x4 == 0)    
	{ModBus_RegisterData[0x0001]=5;}
	else if(hold_x1 == 0 && hold_x2 == 1&& hold_x3 == 1&& hold_x4 == 0)    
	{ModBus_RegisterData[0x0001]=6;}		
	else if(hold_x1 == 1 && hold_x2 == 1&& hold_x3 == 1&& hold_x4 == 0)    
	{ModBus_RegisterData[0x0001]=7;}
	else if(hold_x1 == 0 && hold_x2 == 0&& hold_x3 == 0&& hold_x4 == 1)    
	{ModBus_RegisterData[0x0001]=8;}
	else if(hold_x1 == 1 && hold_x2 == 0&& hold_x3 == 0&& hold_x4 == 1)    
	{ModBus_RegisterData[0x0001]=9;}
	else if(hold_x1 == 0 && hold_x2 == 1&& hold_x3 == 0&& hold_x4 == 1)    
	{ModBus_RegisterData[0x0001]=10;}
	else if(hold_x1 == 1 && hold_x2 == 1&& hold_x3 == 0&& hold_x4 == 1)    
	{ModBus_RegisterData[0x0001]=11;}
	else if(hold_x1 == 0 && hold_x2 == 0&& hold_x3 == 1&& hold_x4 == 1)     
	{ModBus_RegisterData[0x0001]=12;}
	else if(hold_x1 == 1 && hold_x2 == 0&& hold_x3 == 1&& hold_x4 == 1)     
	{ModBus_RegisterData[0x0001]=13;}					
	else if(hold_x1 == 0 && hold_x2 == 1&& hold_x3 == 1&& hold_x4 == 1)    
	{ModBus_RegisterData[0x0001]=14;}						
	else if(hold_x1 == 1 && hold_x2 == 1&& hold_x3 == 1&& hold_x4 == 1)    
	{ModBus_RegisterData[0x0001]=15;}		
}
void write_y(void)
{
	if(Motor_Function_CurrentTorque == 200)
	{
		up = 1;
	}
	else if(Motor_Function_CurrentTorque == 201)  // Motor_Function_CurrentTorque �ı��˸ñ����Ķ���
	{
		down = 1;
	}
	else if(Motor_Function_CurrentTorque == 202)  // Motor_Function_CurrentTorque �ı��˸ñ����Ķ���
	{
		down = 0;
		up = 0;
	}
	if(up == 1 && down == 1)
	{
		up = 0;
		down = 0;
	}
	if(up == 0 && down == 0)
	{
		if (Motor_Function_CurrentTorque==0)   
		{Output_Y1(0); Output_Y2(0);Output_Y3(0);Output_Y4(0);}
		else if (Motor_Function_CurrentTorque==1)   
		{ Output_Y1(1); Output_Y2(0);Output_Y3(0);Output_Y4(0);}
		else if (Motor_Function_CurrentTorque==2)   
		{ Output_Y1(0); Output_Y2(1);Output_Y3(0);Output_Y4(0);}
		else if (Motor_Function_CurrentTorque==3)   
		{ Output_Y1(1); Output_Y2(1);Output_Y3(0);Output_Y4(0);}
		else if (Motor_Function_CurrentTorque==4)   
		{ Output_Y1(0); Output_Y2(0);Output_Y3(1);Output_Y4(0);}
		else if (Motor_Function_CurrentTorque==5)   
		{ Output_Y1(1); Output_Y2(0);Output_Y3(1);Output_Y4(0);}	
		else if (Motor_Function_CurrentTorque==6)   
		{ Output_Y1(0); Output_Y2(1);Output_Y3(1);Output_Y4(0);}	
		else if (Motor_Function_CurrentTorque==7)   
		{ Output_Y1(1); Output_Y2(1);Output_Y3(1);Output_Y4(0);}		
		else if (Motor_Function_CurrentTorque==8)   
		{ Output_Y1(0); Output_Y2(0);Output_Y3(0);Output_Y4(1);}	
		else if (Motor_Function_CurrentTorque==9)   
		{ Output_Y1(1); Output_Y2(0);Output_Y3(0);Output_Y4(1);}	
		else if (Motor_Function_CurrentTorque==10)   
		{ Output_Y1(0); Output_Y2(1);Output_Y3(0);Output_Y4(1);}
		else if (Motor_Function_CurrentTorque==11)   
		{ Output_Y1(1); Output_Y2(1);Output_Y3(0);Output_Y4(1);}
		else if (Motor_Function_CurrentTorque==12)   
		{ Output_Y1(0); Output_Y2(0);Output_Y3(1);Output_Y4(1);}	
		else if (Motor_Function_CurrentTorque==13)   
		{ Output_Y1(1); Output_Y2(0);Output_Y3(1);Output_Y4(1);}
		else if (Motor_Function_CurrentTorque==14)   
		{ Output_Y1(0); Output_Y2(1);Output_Y3(1);Output_Y4(1);}		
		else if (Motor_Function_CurrentTorque==15)  
		{ Output_Y1(1); Output_Y2(1);Output_Y3(1);Output_Y4(1);}
		else if (Motor_Function_CurrentTorque==20)
		{
			if (flash_number < 70)
			{Output_Y1(1);delay_ms(10);}
			else if(flash_number >= 70)
			{Output_Y1(0);delay_ms(10);}
			flash_number++;
			if(flash_number == 140)
				flash_number = 0;
		}
		if (Motor_Function_CurrentTorque==21)   
		{hold_x1 = 0;hold_x2 = 0; hold_x3 = 0; hold_x4 = 0;}
}
}
	
int main(void)
{
	SystemInit(); // ����ϵͳʱ��Ϊ 72M 	
	TIM3_NVIC_Configuration(); // TIM3 ��ʱ���� 
	TIM3_Configuration(); 	
	START_TIME;	 
	
	NVIC_Group_Config();
	LED_Init_Config();
	IOport_Init_Config();
	//CAN_Init_Config();
	RS485_Init_Config();
	TIM2_Init_Config();
	LED_Green(1);
	LED_Red(0);
	//������IO��1Ϊ����������IO��0Ϊ����
	Output_Y1(0);//IO��LED�� �Ƶ�  ����
	Output_Y2(0);//IO��LED�� �̵�
	Output_Y3(0);//IO��LED�� ���
	Output_Y4(0);//IO��LED�� ���
	Timer2_Tick = 0;
	SaveTime = 0;
	delay_ms(4000);
	FLASH_SetLatency(FLASH_Latency_2);
	/* Enable Prefetch Buffer */
	FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
	
	if(*(uint16_t *)0x08007000 == 1)//������FLASH�ڵ�����
	{
		for(int i = 0; i < 0x250; i++)
		{
			ModBus_RegisterData[i] = *(uint16_t *)(0x08007000 + 2*i); 
		}
	}
		
	while(1)
	{
		if(RS485_ReceiveEnd == 1)//������һ������
		{
			RS485_GetData();
			RS485_DataExchange();
			delay_ms(2);
			RS485_ReturnData();
			RS485_RxMessageCnt = 0;
			RS485_ReceiveEnd = 0;
			RS485_CheckFlag = 0;
			RS485_SetFlag = 0;	
			NowGroup=1;
		}
		else
		{
			if(Motor_Function_TryMode == 0 )//无引用 线程无法进入
			{	 
				
				 if(NowGroup==0)       
				 {
					 //hold_x1 = 0;
					 //hold_x2 = 0;
					 //hold_x3 = 0;
					 //hold_x4 = 0;
					 Output_Y1(0);     
					 Output_Y2(0);    
					 Output_Y3(0);
				 } 
				
				 check_X();			// ��IO_X			
				 write_y();			// дIO_Y
			 
			}	
		}
	}
}
